package com.hup.utils.commons.exception.parser;

import com.hup.utils.commons.exception.ErrParserRegister;
import com.hup.utils.commons.exception.ExceptionMsgParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hugan
 * @date 2019/8/24
 * @apiNote 未完成
 */
public class SpringDaoMsgParser implements ErrParserRegister {
    private static final Logger LOGGER = LoggerFactory.getLogger(SpringDaoMsgParser.class);

    @Override
    public List<ExceptionMsgParser> getParsers() {
        //list.add(new Dao());
        return new ArrayList<>();
    }

    /*
    private static class Dao implements ExceptionMsgParser<Throwable> {
        @Override
        public boolean isMatch(Throwable e) {
            return e.getClass().getName().startsWith("org.springframework.dao");
        }

        @Override
        public String getMsg(Throwable e) {
            String errMsg;
            Throwable cause;
            String tag = "数据库异常,";
            if ((cause = e.getCause()) instanceof DataException
                    && (cause = cause.getCause()) instanceof SQLServerException) {
                //尝试解析'数据超长'的异常信息
                errMsg = cause.getMessage();
                if (errMsg != null && errMsg.contains("截断")) {
                    return  tag + "数据长度超出范围。";
                }
                e = cause;
            } else if ((cause = e.getCause()) instanceof org.hibernate.exception.ConstraintViolationException
                    && (cause = cause.getCause()) instanceof SQLServerException) {
                errMsg = cause.getMessage();
                if (errMsg != null) {
                    if (errMsg.contains("语句") && errMsg.contains("冲突")) {
                        LOGGER.error("getErrMsg.原始信息:{}", errMsg);
                        return  tag + cause.getMessage();
                    }
                    if (errMsg.contains("列不允许有 Null ")) {
                        return tag + errMsg.substring(0, errMsg.indexOf('表') - 1);
                    }
                }
                e = cause;
            } else if ((cause = e.getCause()) instanceof JDBCException
                    && (cause = cause.getCause()) instanceof SQLServerException) {
                //一般的数据库异常都是 Throwable e(cause=JDBCException(cause=SQLServerException))
                return  tag + cause.getMessage();
            }
            return tag + e.getMessage();
        }
    }/**/
}
